'''
Created 6/20/12
Modified 7/2/12

Instantiates Whole Brain Catalog SparseValueVolumeImporter#x = np.fromfile('svvdata.txt', dtype=float, sep=' ')
#print x

Run via jython
@author: drlittle
'''

# import Java class
from java.net import URI

# import Java classes from WBC
from org.wholebrainproject.wbc.data.importer import SparseValueVolumeImporter
from org.wholebrainproject.wbc.generated.sparsevaluevolume import SparseValueVolume

'''
SCRIPT BEGINS HERE
'''

# uri to obtain gene expression sparse value volume data from Allen Brain Institute via INCF ABA hub process
svv_uri_string = "http://incf-dev-local.crbs.ucsd.edu/aba/atlas?service=WPS&version=1.0.0&request=Execute&Identifier=GetGeneExpressionByGeneId&DataInputs=geneIdentifier=Coch&RawDataOutput=SparseValueVolumeXML" 

# instantiate Java uri object
svv_uri = URI(svv_uri_string)
print 'uri:', svv_uri.toString()

# instantiate WBC importer
svv_importer = SparseValueVolumeImporter(svv_uri)

# get SparseValueVolume object
sparse_value_volume = svv_importer.getData()

# svv comment string
print 'Sparse Value Volume comment:', sparse_value_volume.getComment()

# get all svv data
sparse_volume_data_type = sparse_value_volume.getSparseVolumeData()

# get list of svv datum (x, y, z, value)
datum_type = sparse_volume_data_type.getData()

# iterate over list of svv datum and write to text file
f = open('svvdata.txt', 'w')
for svd in datum_type:
	f.write(str(svd.getX()))
	f.write(' ')
	f.write(str(svd.getY()))
	f.write(' ')
	f.write(str(svd.getZ()))
	f.write(' ')
	f.write(str(svd.getValue()))
	f.write(' ')
f.close()

